﻿package mortal.game.scene3D.ai.singleAIs.autofight
{
    import Message.BroadCast.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.core.frame.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gLinkText.*;
    import mortal.game.cache.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.fight.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.systemSetting.*;
    import mortal.game.view.task.*;
    import mortal.game.view.task.data.*;

    public class AutoFightBossSelectAI extends Object
    {
        public var rangRect:Rectangle;
        public var isOnlyInRange:Boolean = false;
        private var _au:Vector.<SEntityId>;
        private var _ap:Vector.<SEntityId>;
        private var _am:Vector.<SEntityId>;
        private var _myPoint:Point;
        private var _range:int;
        private var _sourcePoint:Point;
        private var _lastSortAttackTime:int = 0;
        private var _entityMap:EntityDictionary;
        public var priorIds:Dictionary;
        public var onlyIds:Dictionary;
        public var taskBossDic:Dictionary;
        private static var _instance:AutoFightBossSelectAI;

        public function AutoFightBossSelectAI()
        {
            this._myPoint = new Point();
            this._sourcePoint = new Point();
            this.taskBossDic = new Dictionary();
            return;
        }// end function

        public function get sourcePoint() : Point
        {
            return this._sourcePoint;
        }// end function

        public function set sourcePoint(param1:Point) : void
        {
            this._sourcePoint = param1;
            return;
        }// end function

        public function get range() : int
        {
            return this._range;
        }// end function

        public function set range(param1:int) : void
        {
            this._range = param1;
            return;
        }// end function

        public function updateTaskBossCodes() : void
        {
            var _loc_3:TaskInfo = null;
            var _loc_4:Array = null;
            var _loc_5:int = 0;
            var _loc_6:GLinkTextData = null;
            var _loc_7:Array = null;
            var _loc_8:String = null;
            this.taskBossDic = new Dictionary();
            var _loc_1:* = Cache.instance.task;
            var _loc_2:* = _loc_1.taskDoing;
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = _loc_1.getCurLinkTextDatas(_loc_3);
                if (_loc_4 != null)
                {
                    _loc_5 = 0;
                    while (_loc_5 < _loc_4.length)
                    {
                        
                        _loc_6 = _loc_4[_loc_5];
                        if (_loc_6.isNewCollectType())
                        {
                            _loc_7 = _loc_6.subType.split(";");
                            for each (_loc_8 in _loc_7)
                            {
                                
                                this.taskBossDic[int(_loc_8)] = true;
                            }
                        }
                        else if (_loc_6.type == GLinkTextData.boss && _loc_6.value1 > 0)
                        {
                            this.taskBossDic[_loc_6.value1] = true;
                        }
                        _loc_5++;
                    }
                }
            }
            return;
        }// end function

        public function getEntity(param1:Boolean = false, param2:Array = null) : IEntity
        {
            var _loc_3:IEntity = null;
            var _loc_4:int = 0;
            var _loc_7:* = undefined;
            var _loc_8:int = 0;
            var _loc_9:SEntityInfo = null;
            var _loc_10:SEntityId = null;
            var _loc_11:Dictionary = null;
            this.updateMyPoint();
            var _loc_5:* = ThingUtil.entityUtil.entitysMap.getEntityByRangle(this.rangRect, 0, this.isOnlyInRange);
            _loc_5.sort(AutoFightBossSelectAI.instance.sortByDistance);
            var _loc_6:Boolean = false;
            if (this.onlyIds != null)
            {
                for each (_loc_7 in this.onlyIds)
                {
                    
                    _loc_6 = true;
                }
            }
            if (_loc_6)
            {
                if (ThingUtil.selectEntity != null && ThingUtil.selectEntity is MonsterPlayer)
                {
                    if (this.onlyIds[(ThingUtil.selectEntity as MonsterPlayer).bossInfo.code] == true)
                    {
                        if (this.isAttackable(ThingUtil.selectEntity, false))
                        {
                            return ThingUtil.selectEntity;
                        }
                    }
                }
                _loc_4 = 0;
                while (_loc_4 < _loc_5.length)
                {
                    
                    _loc_3 = _loc_5[_loc_4];
                    if (_loc_3 is MonsterPlayer)
                    {
                        if (this.isAttackable(_loc_3, false))
                        {
                            if (this.onlyIds[(_loc_3 as MonsterPlayer).bossInfo.code] == true)
                            {
                                return _loc_3;
                            }
                        }
                    }
                    _loc_4++;
                }
                if (param2 != null)
                {
                    return this.getMinDisBoss(_loc_5, param2);
                }
                return null;
            }
            if (param2 != null)
            {
                _loc_3 = this.getMinDisBoss(_loc_5, param2);
                if (_loc_3 != null)
                {
                    return _loc_3;
                }
            }
            if (param1 && ThingUtil.selectEntity is UserPlayer || !param1 && ThingUtil.selectEntity is MonsterPlayer)
            {
                if (ThingUtil.selectEntity != null && ThingUtil.selectEntity.entityInfo != null && !ThingUtil.selectEntity.entityInfo.isDead)
                {
                    _loc_8 = GeomUtil.calcDistance(ThingUtil.selectEntity.x2d, ThingUtil.selectEntity.y2d, this._myPoint.x, this._myPoint.y);
                    if (_loc_8 <= SceneRange.display.width / 2 + 200)
                    {
                        _loc_9 = ThingUtil.selectEntity.entityInfo.entityInfo;
                        if (!EntityRelationUtil.isFriend(_loc_9))
                        {
                            return ThingUtil.selectEntity;
                        }
                    }
                }
            }
            this._au = FightEffectUtil.attackMeMonsterList;
            if (param1)
            {
                this._au = this._au.concat(FightEffectUtil.attackMeUserList, FightEffectUtil.attackMePetList);
            }
            if (FrameManager.flashFrame.getTime() - this._lastSortAttackTime >= 800)
            {
                this._entityMap = ThingUtil.entityUtil.entitysMap;
                this._au.sort(AutoFightBossSelectAI.instance.sortAuByDistance);
            }
            _loc_4 = 0;
            while (_loc_4 < this._au.length)
            {
                
                _loc_10 = this._au[_loc_4];
                _loc_3 = ThingUtil.entityUtil.getEntity(_loc_10);
                if (_loc_3 is MonsterPlayer)
                {
                    if (this.isAttackable(_loc_3))
                    {
                        return _loc_3;
                    }
                }
                _loc_4++;
            }
            if (!ClientSetting.local.getIsDone(IsDoneType.SelectTaskBoss) && !Cache.instance.copy.isInCopy)
            {
                _loc_11 = Cache.instance.autoFight.getCurMapTaskBoss();
                _loc_4 = 0;
                while (_loc_4 < _loc_5.length)
                {
                    
                    _loc_3 = _loc_5[_loc_4];
                    if (_loc_3 is MonsterPlayer && MonsterPlayer(_loc_3).bossInfo != null)
                    {
                        if (_loc_11[MonsterPlayer(_loc_3).bossInfo.code] == true)
                        {
                            if (this.isAttackable(_loc_3))
                            {
                                return _loc_3;
                            }
                        }
                    }
                    _loc_4++;
                }
            }
            _loc_4 = 0;
            while (_loc_4 < _loc_5.length)
            {
                
                _loc_3 = _loc_5[_loc_4];
                if (_loc_3 is MonsterPlayer)
                {
                    if (Cache.instance.autoFight.isBossSelected((_loc_3 as MonsterPlayer).bossInfo.code))
                    {
                        if (this.isAttackable(_loc_3))
                        {
                            return _loc_3;
                        }
                    }
                }
                _loc_4++;
            }
            return this.getMinDisBoss(_loc_5);
        }// end function

        private function getMinDisBoss(param1:Array, param2:Array = null) : IEntity
        {
            var _loc_4:IEntity = null;
            var _loc_5:IEntity = null;
            var _loc_7:int = 0;
            var _loc_3:int = 1000000;
            var _loc_6:int = 0;
            while (_loc_6 < param1.length)
            {
                
                _loc_5 = param1[_loc_6];
                if (_loc_5 is MonsterPlayer)
                {
                    if (this.isAttackable(_loc_5))
                    {
                        if (param2 != null && param2.indexOf(_loc_5) >= 0)
                        {
                        }
                        else
                        {
                            _loc_7 = this.calDistaneMe(_loc_5);
                            if (_loc_7 > 400 || _loc_5.entityInfo.bodySize >= _loc_7 + 20)
                            {
                            }
                            else
                            {
                                return _loc_5;
                            }
                        }
                    }
                }
                _loc_6++;
            }
            if (param2 != null)
            {
                return param2[0];
            }
            return null;
        }// end function

        public function calDistaneMe(param1:IEntity) : Number
        {
            if (param1 == null)
            {
                return 10000000;
            }
            return GeomUtil.calcDistance(param1.x2d, param1.y2d, this._myPoint.x, this._myPoint.y);
        }// end function

        public function updateMyPoint() : void
        {
            if (GameMapUtil.curMapState.isPetBreakMap)
            {
                this._myPoint.x = ThingUtil.entityUtil.getSelfPetEntity().x2d;
                this._myPoint.y = ThingUtil.entityUtil.getSelfPetEntity().y2d;
            }
            else
            {
                this._myPoint.x = RolePlayer.instance.x2d;
                this._myPoint.y = RolePlayer.instance.y2d;
            }
            return;
        }// end function

        public function sortAuByDistance(param1:SEntityId, param2:SEntityId) : int
        {
            var _loc_3:* = this.calDistaneMe(this._entityMap.getEntity(param1));
            var _loc_4:* = this.calDistaneMe(this._entityMap.getEntity(param2));
            if (_loc_3 <= _loc_4)
            {
                return -1;
            }
            return 1;
        }// end function

        public function sortByDistance(param1:IEntity, param2:IEntity) : int
        {
            var _loc_3:* = this.calDistaneMe(param1);
            var _loc_4:* = this.calDistaneMe(param2);
            if (_loc_3 <= _loc_4)
            {
                return -1;
            }
            return 1;
        }// end function

        public function isAttackable(param1:IEntity, param2:Boolean = true) : Boolean
        {
            if (param1.entityInfo == null || param1.entityInfo.isInHidden)
            {
                return false;
            }
            if (param1 is PetPlayer)
            {
                return false;
            }
            if (param1 is MonsterPlayer)
            {
                if (param1.entityInfo.entityInfo.life <= 0)
                {
                    return false;
                }
                if (BossRule.isCollectBoss(param1 as MonsterPlayer))
                {
                    if (CopyUtil.isInZZXYBattleCopy || CopyUtil.isInSixBossCopy)
                    {
                    }
                    else if (!this.taskBossDic[(param1 as MonsterPlayer).bossInfo.code])
                    {
                        return false;
                    }
                }
            }
            if (param2 && param1 is MonsterPlayer)
            {
                if (!Cache.instance.autoFight.isBossSelected((param1 as MonsterPlayer).bossInfo.code))
                {
                    return false;
                }
            }
            if (param1.x2d <= 0 && param1.y2d <= 0)
            {
                return false;
            }
            if (!this.isInDistance(param1))
            {
                return false;
            }
            if (param1.entityInfo == null || param1.entityInfo.isDead)
            {
                return false;
            }
            if (!FightUtil.isNormalAttackable(param1))
            {
                return false;
            }
            return true;
        }// end function

        private function isInDistance(param1:IEntity) : Boolean
        {
            if (this._range <= 0)
            {
                return true;
            }
            if (GeomUtil.calcDistance(SpritePlayer(param1).x2d, SpritePlayer(param1).y2d, this._sourcePoint.x, this._sourcePoint.y) < this._range)
            {
                return true;
            }
            return false;
        }// end function

        private function isAttakMe(param1:IEntity) : Boolean
        {
            if (this._au.indexOf(param1.entityInfo.entityInfo.entityId) >= 0)
            {
                return true;
            }
            if (this._ap.indexOf(param1.entityInfo.entityInfo.entityId) >= 0)
            {
                return true;
            }
            if (this._am.indexOf(param1.entityInfo.entityInfo.entityId) >= 0)
            {
                return true;
            }
            return false;
        }// end function

        public static function get instance() : AutoFightBossSelectAI
        {
            if (_instance == null)
            {
                _instance = new AutoFightBossSelectAI;
            }
            return _instance;
        }// end function

    }
}
